<?php


namespace app\common\model;

use app\common\model\Common as CommonModel;

class Order extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['title']) && !empty($post['title'])){
            $where[] = ['title','like','%'.$post['title'].'%'];
        }

        if(isset($post['pay_status']) && !empty($post['pay_status']) ){
            $where[] = ['pay_status','=',$post['pay_status']];
        }

        if(isset($post['user_id']) && !empty($post['user_id'])){
            $where[] = ['user_id','=',$post['user_id']];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        $pay_status = config("app.pay_status");
        $Category = new Category();
        $lists = new Lists();
        foreach($list as $k=>$v){
//            $cate_one = $Category -> where('id','=',$v['type'])->find();
//            $list[$k]['cate_name'] = $cate_one['catname'];
            $lists_one = $lists -> where('id','=',$v['lists_id'])->find();
            $cate_one = $Category->where('id','=',$lists_one['cate_id'])->find();

            $list[$k]['cate_name'] = $cate_one['catname'];
            $list[$k]['add_time'] = date("Y-m-d H:i:s",$v['add_time']);
            $list[$k]['pay_status_name'] = $pay_status[$v['pay_status']];
        }

        return $list;
    }


    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];

        $data['user_id'] = isset($param['user_id'])? $param['user_id'] : cookie('user_id');
        if(isset($param['lists_id'])) {
            $data['lists_id'] = $param['lists_id'];
            $lists = new Lists();
            $lists_one = $lists->where('id', '=', $param['lists_id'])->find();
            $data['img_url'] = $lists_one['img_url'];
            $data['type'] = $lists_one['type'];
            $data['title'] = $lists_one['title'];
            $data['price'] = $lists_one['price'];
        }

        if(isset($param['pay_code'])) {
            $data['pay_code'] = $param['pay_code'];
            if (isMobile()) {
                $duan = 'mobile端';
            } else {
                $duan = 'pc端';
            }
            if ($param['pay_code'] == 'alipay') {
                $type = '支付宝';
            } else {
                $type = '微信';
            }
            $data['pay_name'] = $duan . $type;
        }
        if(isset($param['pay_name'])){
            $data['pay_name'] = $param['pay_name'];
        }

        $data['take_name'] = isset($param['take_name'])?$param['take_name']:'';
        $data['take_mobile'] = isset($param['take_mobile'])?$param['take_mobile']:'';
        $data['take_address'] = isset($param['take_address'])?$param['take_address']:'';
        if(isset($param['pay_status'])){
            $data['pay_status'] = $param['pay_status'];
        }

        if(isset($param['pay_time'])){
            $data['pay_time'] = $param['pay_time'];
        }
        if($act == 'add'){
            $data['order_sn'] = date('YmdHis',time()).rand(1000,9999);
            $data['add_time'] = isset($param['add_time']) ? strtotime($param['add_time']) : time();
            $r = $this->save($data);
            $order_id = $this->id;
        }
        if($act == 'edit'){
            $data['update_time'] = time();
            $r = $this->where('id','=',$param['id'])->update($data);
            $order_id = $param['id'];
        }
        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        $result['data'] = ['order_id'=>$order_id];
        return $result;
    }

    public function info($id){
        $info = $this->where('id','=',$id)->find();
        $Category = new Category();
        $cate_one = $Category->where('id','=',$info['type'])->find();
        $info['cate_name'] = $cate_one['catname'];
        $info['add_time_o'] = $info['add_time'];
        $info['add_time'] = date("Y-m-d",$info['add_time']);
        $pay_status = config("app.pay_status");
        $info['pay_status_name'] = $pay_status[$info['pay_status']];
        return $info;
    }


}